package com.easybuy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easybuy.pojo.User;
import com.easybuy.service.UserService;
import com.easybuy.util.Code;
import com.easybuy.util.PageBean;
import com.mysql.jdbc.StringUtils;


@Controller
@RequestMapping(value="/user")
public class UserController {

	@Resource
	private UserService userService;
	
	@RequestMapping(value="/userexist")
	@ResponseBody
	public Object userIsExist(@RequestParam String loginName) {
		Map<String, String> result = new HashMap<String, String>();
		if(loginName!=null && loginName!="") {
			User user = userService.selectUserExist(loginName);
			if(user==null) {
				result.put("loginName", "noexist");
			}else {
				result.put("loginName", "exist");
			}
		}else {
			result.put("loginName", "null");
		}
		return result;
	}
	/**
	 *      用户注册
	 * @param user
	 * @return success/failed
	 */
	@RequestMapping(value="/insert")
	@ResponseBody
	public Object insertuser(User user) {
		Map<String, String> result = new HashMap<String, String>();
		Code code = new Code();
		String passString = code.transferCode(user.getPassword());
		//System.out.println(passString);
		user.setType(0);
		user.setPassword(passString);
		int updateRows = userService.insertUser(user);
		if(updateRows>0) {
			result.put("regist", "success");
		}else {
			result.put("regist", "failed");
		}
		return result;
	}
	/**
	 * 用户登录
	 * @param loginName
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Object login(@RequestParam(value="loginName",required=false) String loginName , 
						@RequestParam(value="password",required=false)String password) {
		Map<String, Object> result = new HashMap<String, Object>();
		User user = null;
		Code code = new Code();
		//System.out.println(loginName);
		//System.out.println(password);
		String pasString = code.OnceTransfer(password);
		//System.out.println(pasString);
		user = userService.login(loginName, password);
		String userName = user.getUserName();
		//System.out.println(userName);
		//System.out.println(user.getPassword());
		if(user.getPassword().equalsIgnoreCase(pasString)) {
			result.put("ylogin", "success");
			result.put("userName", userName);
			result.put("userId", user.getId());
		}else {
			result.put("nlogin", "failed");
			//result.put("userName", userName);
		}
		return result;
	}
	/**
	 * 查询用户数量
	 */
	@RequestMapping(value = "/count")
	@ResponseBody
	public Integer selectUserCount() {
		Integer count = userService.selectUserCount();
		return count;
	}
	/**
	 * 查询用户列表
	 * @param pageNow
	 * @return
	 */
	
	@RequestMapping(value = "/userList")
	@ResponseBody
	public Object selectUserByPage(@RequestParam(value = "pageIndex",required = false)String pageIndex) {
	
		PageBean<User> pagebean = new PageBean<User>();
		int count = userService.selectUserCount();
		pagebean.setCount(count);
		int pageNo = -1;
		//设置当前页面
		if(pageIndex!=null && pageIndex.length()>0) {
			pageNo = Integer.parseInt(pageIndex);
		}else {
			pageNo = 1;
		}
		
		pagebean.setPageNow(pageNo);
		pagebean.setPageSize(5);
		int pagecount = count%5==0?count/5:count/5+1;
		pagebean.setPageCount(pagecount);
		Map<String, Object> params = new HashMap<String,Object>();
		if(!(params.containsKey("pageNow") && params.containsKey("pageSize"))) {
			params.put("pageNow", (pagebean.getPageNow()-1)*pagebean.getPageSize());
			params.put("pageSize", pagebean.getPageSize());
		}
		
		List<User> userList = userService.selectUserByPage(params);
		pagebean.setList(userList);
		return pagebean;
	}
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Integer DeleteUser(@RequestParam(value = "id")String id) {
		Integer userId = Integer.parseInt(id); 
		int updateRow = userService.DelUerById(userId);
		return updateRow;
	}
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Integer UpdateUser(User user) {
		Integer updateRows = userService.updateUser(user);
		return updateRows;
	}
	/**
	 * 获取单个用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getUser")
	@ResponseBody
	public Object selectUserById(@RequestParam(value = "id") String id) {
		Integer userId = Integer.parseInt(id);
		return userService.selectUserById(userId);
		
	}
}
